package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.shopping.models.bo.shop.ShopAddressBO;
import com.ztgf.sys.models.dto.SysAreaDTO;
import com.ztgf.sys.models.service.ISysAreaService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopAddressMapper;
import com.ztgf.shopping.models.service.shop.IShopAddressService;
import com.ztgf.shopping.models.bo.shop.ShopAddressSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopAddressDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 用户常用地址表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 11:11:21
 */
@DubboService
public class ShopAddressServiceImpl extends ServiceImpl<ShopAddressMapper, ShopAddressDTO> implements IShopAddressService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private ShopAddressMapper shopAddressMapper;
    @DubboReference
    private ISysAreaService iSysAreaService;

    @Override
    public boolean add(ShopAddressSaveBO saveBO) {
        ShopAddressDTO dto = new ShopAddressDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        if (dto.getAreaId().equals(0l)){
            dto.setCompleteAddress("全国" + dto.getAddress());
        }else {
            dto.setCompleteAddress(iSysAreaService.getParentArea(dto.getAreaId(),new StringBuffer()).toString() + dto.getAddress());
        }
        return this.insert(dto);
    }

    @Override
    public boolean updateDefault(Long id) {
        ShopAddressDTO shopAddressDTO = this.selectById(id);
        ShopAddressDTO newAddressDTO = new ShopAddressDTO();
        Wrapper<ShopAddressDTO> wrapper = new EntityWrapper<>();
        //获取旧的dto
        ShopAddressDTO oldShopAddressDTO = this.selectOne(wrapper.eq("shop_id", shopAddressDTO.getShopId()).eq("is_default", 1).eq("type", shopAddressDTO.getType()));
        if (oldShopAddressDTO != null){
            ShopAddressDTO oldAddressDTO = new ShopAddressDTO();
            oldAddressDTO.setIsDefault(0);//改为不默认；
            oldAddressDTO.setId(oldShopAddressDTO.getId());
            this.updateById(oldAddressDTO);
        }
        newAddressDTO.setIsDefault(1);//改为默认
        newAddressDTO.setId(id);
        return this.updateById(newAddressDTO);
    }

    /**
     * 获取所有的买家地址 默认地址为第一个
     * @return
     */
    @Override
    public List<ShopAddressDTO> getAllBuyerAddress(Long shopId){
        Wrapper<ShopAddressDTO> wrapper = new EntityWrapper();
        wrapper.orderBy("is_default",false);//倒序
        wrapper.eq("type", 0);//买家收货地址
        wrapper.eq("shop_id", shopId);//店铺地址
        return shopAddressMapper.selectList(wrapper);
    }

    @Override
    public Long updateAddress(ShopAddressSaveBO saveBO) {
        ShopAddressDTO dto = new ShopAddressDTO();
        BeanUtils.copyProperties(saveBO, dto);

        if (dto.getAreaId().equals(0l)){
            dto.setCompleteAddress("全国" + dto.getAddress());
        }else {
            dto.setCompleteAddress(iSysAreaService.getParentArea(dto.getAreaId(),new StringBuffer()).toString() + dto.getAddress());
        }

        if (saveBO.getId() == null){
            Long id = ymlConfig.getPkId();
            dto.setId(id);
            this.insert(dto);
            return id;
        }else {
            dto.setId(saveBO.getId());
            this.updateById(dto);
            return saveBO.getId();
        }
    }
}
